<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="form" label-width="200px" v-if="open_type != 'psw'">
      <el-form-item label="问题名称" required prop="title">
        <el-input class="max-w460" v-model="form.title" placeholder="请输入问题名称"></el-input>
      </el-form-item>
      <el-form-item label="问题名称" prop="description">
        <el-input
          type="textarea"
          class="max-w460"
          v-model="form.description"
          placeholder="请输入问题描述"
        ></el-input>
      </el-form-item>
      <el-form-item label="展示状态">
        <el-radio-group v-model="form.is_show">
          <el-radio :value="1">打开</el-radio>
          <el-radio :value="0">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确定</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { addProblemApi, updateProblemApi } from "@/api/babycar";
import { ElMessage } from "element-plus";
import { ref } from "vue";
const title = ref("编辑问题");
const dialogVisible = ref(false);
const form = ref({
  problem_id: 0,
  type: "常见问题",
  title: "",
  description: "",
  is_show: 1,
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  loading.value = false;
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (type: string, row?: any) => {
  open_type.value = type;
  console.log(row);
  if (type == "edit") {
    title.value = "编辑问题";
    form.value = row;
  } else if (type == "add") {
    title.value = "添加问题";
    form.value = {
      ...{
        problem_id: 0,
        type: "常见问题",
        title: "",
        description: "",
        is_show: 1,
      },
      ...row,
    };
  }
  dialogVisible.value = true;
};
const emits = defineEmits(["doneSave"]);
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      if (open_type.value == "add") {
        addProblemApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "添加成功",
              type: "success",
            });
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      } else if (open_type.value == "edit") {
        updateProblemApi(form.value)
          .then((data: any) => {
            ElMessage({
              message: "修改成功",
              type: "success",
            });
            loading.value = false;
            dialogFormVisible();
            emits("doneSave");
          })
          .catch((error) => {
            loading.value = false;
          });
      }
    }
  });
};
/*取消*/
const cancelFunc = () => {
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style></style>
